package com.yun.zf.system.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yun.zf.common.admin.domain.SysUserPhone;
import com.yun.zf.system.mapper.SysUserPhoneMapper;
import com.yun.zf.system.security.utils.SecurityUtils;
import com.yun.zf.system.service.ISysUserService;
import com.yun.zf.system.service.SysUserPhoneService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * <p>
 * 用户id,手机号关联表 服务实现类
 * </p>
 *
 * @author cj
 * @since 2021-08-23
 */
@Service
public class SysUserPhoneServiceImpl extends ServiceImpl<SysUserPhoneMapper, SysUserPhone> implements SysUserPhoneService {

    @Autowired
    private ISysUserService userService;
    @Override
    @Transactional
    public int bindPhone(String iphone) {
        Long userId = SecurityUtils.getLoginUser().getUserId();
        String openId = SecurityUtils.getLoginUser().getOpendId();
        SysUserPhone phone = new SysUserPhone();
        phone.setIphone(Long.parseLong(iphone));
        phone.setOpenId(openId);
        phone.setUserId(userId);
        int c = baseMapper.insert(phone);
        if (c==0){
            return  -1;
        }
        // 绑定成功后更新 用户表
        userService.updateUserNameByUserId(userId,iphone);
        return  c;
    }
    @Override
    public SysUserPhone getByIphone(Long iphone) {
        QueryWrapper<SysUserPhone> wrapper = new QueryWrapper<>();
        wrapper.lambda().eq(SysUserPhone::getIphone,iphone);
        return  baseMapper.selectOne(wrapper);
    }
}
